﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ObjectSweptVolume.CObjectSweptVolumeDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.ObjectSweptVolume;

public class CObjectSweptVolumeDlg : TxWindow, IComponentConnector
{
  private CObjectSweptVolumeViewModel _model;
  private bool _loadingWindow = true;
  private bool _restoreOldCollectDataState;
  internal CObjectSweptVolumeDlg OSVMainDlg;
  internal System.Windows.Controls.Image m_addBtn;
  internal System.Windows.Controls.Image m_removeBtn;
  internal DataGrid ResourcesDataGrid;
  internal TextBlock AccuracyUnitTextBlock;
  internal Slider AccuracySlider;
  internal Label SafetyClearanceLabel;
  internal ToggleButton CollectBtn;
  internal Button CloseBtn;
  internal Button CreateBtn;
  private bool _contentLoaded;

  public CObjectSweptVolumeDlg()
  {
    this.InitializeComponent();
    TxApplication.Options.Units.LinearUnitsChanged += new TxUnitsOptions_LinearUnitsChangedEventHandler(this.Units_LinearUnitsChanged);
    this.UpdateLinearUnitText();
    this.m_addBtn.Source = TxImageManager.GetImageByKey("AddToTable").ConvertTo<ImageSource>(new System.Drawing.Size());
    this.m_removeBtn.Source = TxImageManager.GetImageByKey("RemoveFromTable").ConvertTo<ImageSource>(new System.Drawing.Size());
    this._model = new CObjectSweptVolumeViewModel();
    this._model.PropertyChanged += new PropertyChangedEventHandler(this.ModelView_PropertyChanged);
    this._model.ItemSelectionForced += new CObjectSweptVolumeViewModel.ItemSelectionForcedEventHandler(this.ModelView_ItemSelectionForced);
    ((FrameworkElement) this).DataContext = (object) this._model;
  }

  private void ModelView_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (!(sender is CObjectSweptVolumeViewModel) || !(e.PropertyName == "IsCreationExecuting"))
      return;
    if (this._model.IsCreationExecuting)
    {
      if (!this._model.ShouldCollectData)
        return;
      this._model.ShouldCollectData = false;
      this._restoreOldCollectDataState = true;
    }
    else
    {
      if (!this._restoreOldCollectDataState)
        return;
      this._model.ShouldCollectData = true;
      this._restoreOldCollectDataState = false;
    }
  }

  private void ModelView_ItemSelectionForced(
    object sender,
    CObjectSweptVolumeViewModel.ItemSelectionForcedEventArgs e)
  {
    this.SetSelection(this.ResourcesDataGrid, e.Rows);
  }

  private void SetSelection(DataGrid dataGrid, IEnumerable<int> rows)
  {
    dataGrid.SelectedItems.Clear();
    foreach (int row in rows)
    {
      if (row >= 0 && row <= dataGrid.Items.Count - 1)
      {
        object obj = dataGrid.Items[row];
        dataGrid.SelectedItems.Add(obj);
        if (!(dataGrid.ItemContainerGenerator.ContainerFromIndex(row) is DataGridRow rowContainer))
        {
          dataGrid.ScrollIntoView(obj);
          rowContainer = dataGrid.ItemContainerGenerator.ContainerFromIndex(row) as DataGridRow;
        }
        if (rowContainer != null)
          this.GetCell(dataGrid, rowContainer, 0)?.Focus();
      }
    }
  }

  private DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
  {
    if (rowContainer != null)
    {
      DataGridCellsPresenter visualChild = this.FindVisualChild<DataGridCellsPresenter>((DependencyObject) rowContainer);
      if (visualChild == null)
      {
        rowContainer.ApplyTemplate();
        visualChild = this.FindVisualChild<DataGridCellsPresenter>((DependencyObject) rowContainer);
      }
      if (visualChild != null)
      {
        if (!(visualChild.ItemContainerGenerator.ContainerFromIndex(column) is DataGridCell cell))
        {
          dataGrid.ScrollIntoView((object) rowContainer, dataGrid.Columns[column]);
          cell = visualChild.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
        }
        return cell;
      }
    }
    return (DataGridCell) null;
  }

  private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
  {
    for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(obj); ++childIndex)
    {
      DependencyObject child = VisualTreeHelper.GetChild(obj, childIndex);
      if (child != null && child is childItem visualChild1)
        return visualChild1;
      childItem visualChild2 = this.FindVisualChild<childItem>(child);
      if ((object) visualChild2 != null)
        return visualChild2;
    }
    return default (childItem);
  }

  private void CloseBtn_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  public void UpdateLinearUnitText()
  {
    this.SafetyClearanceLabel.Content = (object) (StringTable.OSV_SAFETY_CLEARANCE_LABEL + this.LinearUnitLabelText());
    this.AccuracyUnitTextBlock.Text = TxApplication.Options.Units.LinearUnitName;
  }

  private void Units_LinearUnitsChanged(
    object sender,
    TxUnitsOptions_LinearUnitsChangedEventArgs args)
  {
    this._model.OnLinearUnitChanged();
    this.UpdateLinearUnitText();
  }

  private void OSVMainDlg_Closed(object sender, EventArgs e)
  {
    this._model.OnWindowClosed();
    this._model.PropertyChanged -= new PropertyChangedEventHandler(this.ModelView_PropertyChanged);
    this._model.ItemSelectionForced -= new CObjectSweptVolumeViewModel.ItemSelectionForcedEventHandler(this.ModelView_ItemSelectionForced);
    this._model.Uninitialize();
    TxApplication.Options.Units.LinearUnitsChanged -= new TxUnitsOptions_LinearUnitsChangedEventHandler(this.Units_LinearUnitsChanged);
  }

  private void ToolBar_Loaded(object sender, RoutedEventArgs e)
  {
    ToolBar templatedParent = sender as ToolBar;
    if (templatedParent.Template.FindName("OverflowGrid", (FrameworkElement) templatedParent) is FrameworkElement name1)
      name1.Visibility = Visibility.Collapsed;
    if (!(templatedParent.Template.FindName("MainPanelBorder", (FrameworkElement) templatedParent) is FrameworkElement name2))
      return;
    Thickness thickness = new Thickness();
    name2.Margin = thickness;
  }

  private void Expander_SizeChanged(object sender, SizeChangedEventArgs e)
  {
    if (this._loadingWindow)
      return;
    double num = ((FrameworkElement) this).ActualHeight + (e.NewSize.Height - e.PreviousSize.Height);
    ((FrameworkElement) this).MinHeight = num;
    ((FrameworkElement) this).MaxHeight = num;
  }

  private void OSVMainDlg_Loaded(object sender, RoutedEventArgs e)
  {
    ((FrameworkElement) this).MinWidth = 280.0;
    ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).ActualHeight;
    ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).ActualHeight;
    this._loadingWindow = false;
    this._model.AddPreSelectedItems();
  }

  private string LinearUnitLabelText() => $" ({TxApplication.Options.Units.LinearUnitName}):";

  private void ResourcesDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    ObservableCollection<CApObjectSweptVolumeItem> observableCollection = new ObservableCollection<CApObjectSweptVolumeItem>();
    foreach (CApObjectSweptVolumeItem objectSweptVolumeItem in this.ResourcesDataGrid.SelectedItems.Cast<CApObjectSweptVolumeItem>())
      observableCollection.Add(objectSweptVolumeItem);
    this._model.SelectedItems = observableCollection;
  }

  private void ResourcesDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Delete)
      return;
    this._model.RemoveSelectedItems();
  }

  private void OSVMainDlg_Closing(object sender, CancelEventArgs e)
  {
    if (!this._model.IsCreationExecuting)
      return;
    this._model.ForceAbortCreation();
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/objectsweptvolume/cobjectsweptvolumedlg.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.OSVMainDlg = (CObjectSweptVolumeDlg) target;
        ((Window) this.OSVMainDlg).Closed += new EventHandler(this.OSVMainDlg_Closed);
        ((Window) this.OSVMainDlg).Closing += new CancelEventHandler(this.OSVMainDlg_Closing);
        ((FrameworkElement) this.OSVMainDlg).Loaded += new RoutedEventHandler(this.OSVMainDlg_Loaded);
        break;
      case 2:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.ToolBar_Loaded);
        break;
      case 3:
        this.m_addBtn = (System.Windows.Controls.Image) target;
        break;
      case 4:
        this.m_removeBtn = (System.Windows.Controls.Image) target;
        break;
      case 5:
        this.ResourcesDataGrid = (DataGrid) target;
        this.ResourcesDataGrid.SelectionChanged += new SelectionChangedEventHandler(this.ResourcesDataGrid_SelectionChanged);
        this.ResourcesDataGrid.PreviewKeyDown += new KeyEventHandler(this.ResourcesDataGrid_PreviewKeyDown);
        break;
      case 6:
        ((FrameworkElement) target).SizeChanged += new SizeChangedEventHandler(this.Expander_SizeChanged);
        break;
      case 7:
        this.AccuracyUnitTextBlock = (TextBlock) target;
        break;
      case 8:
        this.AccuracySlider = (Slider) target;
        break;
      case 9:
        this.SafetyClearanceLabel = (Label) target;
        break;
      case 10:
        this.CollectBtn = (ToggleButton) target;
        break;
      case 11:
        this.CloseBtn = (Button) target;
        this.CloseBtn.Click += new RoutedEventHandler(this.CloseBtn_Click);
        break;
      case 12:
        this.CreateBtn = (Button) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
